
var pageNum = 1;
var rowNum = 16;
$(function(){ 
	loadData();
	//setBu	tton();
	
}); 
function loadData(){
	$.ajax({
		  type: 'POST',
		  url: 'item/list',
		  data: {
			  page:pageNum,
			  rows:rowNum
		  },
		  dataType: 'json',
		  success: function(data) {
			  var rowCount = getJsonLength(data);
			  createTableInForData(data);
			}
		});
	$("#cruurepage").empty();
	$("#cruurepage").append(pageNum);
}
function createTableInForData(data){
	//共有多少元素
	var count = getJsonLength(data);
	//alert(count);
	//创建一个表
	var table1 = $("<table border=\"0\" class=\"booklist\" cellspacing=\"0\"> ");
	//把table加到div中
    table1.appendTo($("#createTable"));
    var rowCount = count/4;
    
    //产品计数
    var pIndex = 0;
    for(var i = 0; i<= rowCount ;i++ ){
    	var trHeader = $("<tr></tr>");
    	trHeader.appendTo(table1);
    	for (var j = 0; j <4; j++) {
    		var td = $("<td>" + "<div class=\"divbookpic\"><p><a href=\"/item/"+data[pIndex].id+"\"><img src=\"/images/wu.png\" width=\"115\" height=\"129\" border=\"0\" /> </a></p></div><div class=\"divlisttitle\"><a href=\"/item/"+data[pIndex].id+"\">"+data[pIndex].title + "<br />惊爆价:"+data[pIndex].price/100+".00</a></div></td>");
    		td.appendTo(trHeader);
    		pIndex++;
    	}
    }
}
//计算json对象个数
function getJsonLength(jsonData){
	var jsonLength = 0;
	for(var item in jsonData){
		jsonLength++;

	}
	return jsonLength;

}

function nextPage(){
	var pageNumNew = pageNum+1;
	goPage(pageNumNew)
}
function lastPage(){
	var pageNumNew = pageNum-1;
	if(pageNumNew < 1){
		pageNumNew = 1;
	}
	goPage(pageNumNew)
}
function goPage(pageNumNew){
	clearData();
	pageNum= pageNumNew;
	loadData();
}
function clearData(){
	$("#createTable").empty();
}

function setButton(){
	if(pageNum == 1){
		$("#lastpage").attr("'class", "disablepage"); 
	}
	if(pageNum == 4){
		$("#nextpage").attr("'class", "disablepage"); 
	}
	$("#page"+pageNum).attr("'class", "currentpage"); 
}